package the.broccoli.setting.interfaces.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.broccoli.setting.dto.ParamsCondition;
import the.broccoli.setting.dto.ParamsDTO;
import the.broccoli.setting.entity.Params;
import the.broccoli.setting.service.IParamsService;

import java.util.Date;
import java.util.List;

/**
 * 参数控制器
 */
@RestController
@RequestMapping("/setting/params")
public class ParamsController {

    @Autowired
    private IParamsService paramsService;

    /**
     * 新增参数
     */
    @PostMapping("/add")
    public APIResult add(
            @ApiParam(name = "paramsName", value = "名称", required = true)
            @RequestParam(value = "paramsName", required = true) String paramsName,
            @ApiParam(name = "paramsCode", value = "编码", required = true)
            @RequestParam(value = "paramsCode", required = true) String paramsCode,
            @ApiParam(name = "paramsValue", value = "参数值", required = false)
            @RequestParam(value = "paramsValue", required = false) String paramsValue,
            @ApiParam(name = "remark", value = "备注", required = false)
            @RequestParam(value = "remark", required = false) String remark

    ) {
        Params entity = new Params();
        entity.setParamsName(paramsName);
        entity.setParamsCode(paramsCode);
        entity.setParamsValue(paramsValue);
        entity.setRemark(remark);
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        paramsService.save(entity);
        return APIResult.success();
    }

    /**
     * 删除参数
     */
    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        paramsService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    /**
     * 修改参数
     */
    @PostMapping("/modify")
    public APIResult modify(
            @ApiParam(name = "paramsId", value = "参数ID", required = true)
            @RequestParam(value = "paramsId", required = true) String paramsId,
            @ApiParam(name = "paramsName", value = "名称", required = true)
            @RequestParam(value = "paramsName", required = true) String paramsName,
            @ApiParam(name = "paramsValue", value = "参数值", required = false)
            @RequestParam(value = "paramsValue", required = false) String paramsValue,
            @ApiParam(name = "remark", value = "备注", required = false)
            @RequestParam(value = "remark", required = false) String remark
    ) {
        Params entity = new Params();
        entity.setParamsId(paramsId);
        entity.setParamsName(paramsName);
        entity.setParamsValue(paramsValue);
        entity.setRemark(remark);
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        paramsService.updateById(entity);
        return APIResult.success();
    }

    /**
     * 获取参数信息
     * @param paramsId  参数ID
     * @return
     */
    @GetMapping("/{paramsId}")
    public APIResult<ParamsDTO> get(@PathVariable(value = "paramsId") String paramsId) {
        return APIResult.success(paramsService.detailById(paramsId));
    }

    /**
     * 参数列表
     */
    @GetMapping("/page")
    public APIResult<QueryResult> page(
            @ApiParam(name = "paramsName", value = "名称", required = false)
            @RequestParam(value = "paramsName", required = false) String paramsName,
            @ApiParam(name = "paramsCode", value = "编码", required = false)
            @RequestParam(value = "paramsCode", required = false) String paramsCode,
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        ParamsCondition condition = new ParamsCondition();
        condition.setKeyword(keyword);
        condition.setOrderBy(orderBy);
        condition.setParamsName(paramsName);
        condition.setParamsCode(paramsCode);
        return APIResult.success(paramsService.pageList(condition, pageNum, pageSize));
    }

    /**
     * 获取参数信息
     * @param paramsCode    参数编码
     * @return
     */
    @GetMapping("/value/{paramsCode}")
    public APIResult getValue( @ApiParam(name = "paramsCode", value = "参数编码") @PathVariable(value = "paramsCode") String paramsCode) {
        return APIResult.success(paramsService.getValue(paramsCode));
    }
}